package com.wjq.auth2.service.impl;

import com.wjq.auth2.entity.TUser;
import com.wjq.auth2.mapper.UserMapper;
import com.wjq.auth2.service.IUserService;
import org.springframework.security.core.authority.AuthorityUtils;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wjq
 * @since 2020-05-21
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, TUser> implements IUserService, UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        log.info("用户进行认证的用户名为：{}",s);
        TUser user = new TUser();
        user.setUsername(s);
        TUser user1 = this.baseMapper.selectOne(user);
        if (user1==null){
            throw  new UsernameNotFoundException("用户名不存在");
        }
        return new User(s,user1.getPassword(), AuthorityUtils.createAuthorityList("admin"));
    }
}
